﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapQVW_ABFOLGE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQVW_ABFOLGE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryQVW_ABFOLGE(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapQVW_ABFOLGE(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertQVW_ABFOLGE(dwData, 1);
            }
            UpdateQVW_ABFOLGE(dwLocal);

        }
        public void InsertQVW_ABFOLGE(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1://20140501
                    or1.CommandText = "INSERT INTO QVW_ABFOLGE  "
                        + "SELECT  DISTINCT  'V'||ABFAUFKRS||ABFAUFLFD AS KAUF,'V' AS AUFTYP, ABFAUFKRS AS PKRS, ABFAUFLFD AS PLFD,ABFAUFPOS AS PPOS,'V'||ABFAUFKRS||ABFAUFLFD||ABFAUFPOS AS PAUF,ABFAFOLGE AS PFSTEP,ABFSTATUS,ABFKSTKRS,ABFKST,ABFKSTKRS||ABFKST AS KST,ABFAGANG AS KOPERATION,SUBSTR(SDDATEN,2,10) AS KOPEBEZ,ABFMTYP AS MTYPE,ABFPLANKZ,ABFLIEFNR AS LSNR,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS MACHINE,ABFSOLLSTK,ABFSOLLMG*0.001 as PLANKG,ABFSOLLME,ABFSOLLMTR*0.001 AS PLANMTR,ABFIMG*0.001 AS AKG,ABFIMTR*0.001 AS AMTR,ABFFIX,ABFSOLLSTART AS PFSTART,ABFSOLLDAT AS PFEND,ABFISTARTDAT AS AFDSTART,ABFISTARTZT AS AFTSTART,ABFIENDDAT  AS AFDEND,ABFIENDZT AS AFTEND,'V'||ABFREAUFKRS||ABFREAUFLFD||ABFREAUFPOS AS RPAUF,ABFLO AS LO, ABFLB AS LB,0.0 AS FCOST,0.0 AS MCOST,0.0 AS MBOMCOST "
                        + " FROM ABFOLGE JOIN SDDAT ON ABFAGANG=SUBSTR(SDKEY,3,15) AND SDSA='0231' AND SUBSTR(SDKEY,1,2)=ABFMANDANT "
                        //+ " WHERE ABFMANDANT=:MANDANT  AND ABFNEUDAT>'20140301'";
                        + " WHERE ABFMANDANT=:MANDANT ";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public void DeleteQVW_ABFOLGE(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QVW_ABFOLGE X";
                    break; 
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryQVW_ABFOLGE(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM QVW_ABFOLGE  "; ;

                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "QVW_ABFOLGE";
            Return = Result;
            return Return;
        }
        public void UpdateQVW_ABFOLGE(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "UPDATE QVW_ABFOLGE F SET RPAUF=(SELECT CASE WHEN dbms_random.value(0,1)<0.2 THEN MAX( K.PAUF) ELSE 'V  000000000' END"
                        + " FROM QVW_ABPOS K,QVW_ABPOS PF WHERE PF.PAUF=F.PAUF AND K.PAUF<>F.PAUF)"
                        + " WHERE F.PFSTEP='030'";
                    break;
                           case 2:
                    or1.CommandText = "UPDATE QVW_ABFOLGE F SET FCOST=(SELECT SUM(PRCOST) FROM QVW_KSLEIST KS WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP)  WHERE EXISTS (SELECT 1 FROM QVW_KSLEIST KS WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP)";
                    break;
                           case 3:
                    or1.CommandText = "  UPDATE QVW_ABFOLGE F SET MCOST=(SELECT SUM(STDCOST) FROM QVW_MCOST KS"
                        + " WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP) WHERE EXISTS (SELECT 1 FROM QVW_MCOST KS WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP)";
                    break;
                           case 4:
                    or1.CommandText = "UPDATE QVW_ABFOLGE F SET MBOMCOST=(SELECT SUM(STDCOST) FROM QVW_MCOST KS"
                        + " WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP AND (BS='36' OR BS='37' OR BS='38' OR BS='58')) WHERE EXISTS"
                        + " (SELECT 1 FROM QVW_MCOST KS WHERE F.PKRS=KS.PKRS AND F.PLFD=KS.PLFD AND F.PPOS=KS.PPOS AND F.PFSTEP=KS.PFSTEP AND (BS='36' OR BS='37' OR BS='38' OR BS='58'))";
                    break; 

            }
            Connect2Proxy(or1);
        }
    }
}
